<?php
include_once APPLICATION_PATH."/models/base/Area_Agent_ProductBase.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Area_Agent_Product extends Model_Area_Agent_ProductBase{
    
}
class Model_Area_Agent_ProductMapper extends Model_Area_Agent_ProductMapperBase{
    public static function getFactor($area_id,$agent_id,$product_id,$province_id,$district_id,$date){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select ="SELECT DISTINCT * FROM "
                . "(select * from area_agent_product where agent_id='".$agent_id."' and product_id='".$product_id."' and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    UNION 
                    select * from area_agent_product where agent_id='".$agent_id."' and product_id='".$product_id."' and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    UNION 
                    select * from area_agent_product where (agent_id=0 or agent_id='".$agent_id."') and province_id='".$province_id."' and product_id='".$product_id."' and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    UNION 
                    select * from area_agent_product where (agent_id=0 or agent_id='".$agent_id."') and district_id='".$district_id."' and product_id='".$product_id."' and UNIX_TIMESTAMP(from_date) < '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    UNION 
                    select * from area_agent_product where (agent_id=0 or agent_id='".$agent_id."') and area_id='".$area_id."' and product_id='".$product_id."' and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    UNION 
                    select * from area_agent_product where (agent_id=0 or agent_id='".$agent_id."') and product_id='".$product_id."' and province_id=0 and area_id=0 and district_id=0 and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    ) as factor
                    order by agent_id desc, district_id desc, province_id desc, area_id desc,factor desc
                    LIMIT 1";
        if($area_id==0 && $province_id==0  && $district_id==0){
             $select =" 
                    select * from area_agent_product where (agent_id=0 or agent_id='".$agent_id."') and product_id='".$product_id."' and province_id=0 and area_id=0 and district_id=0 and UNIX_TIMESTAMP(from_date) <= '".$date."' and UNIX_TIMESTAMP(to_date) >= '".$date."' and isdeleted=1
                    LIMIT 1";
        }
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        if(count($rows)==0){
            return 0;
        }
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Area_Agent_Product();
            $entry->setId($row->id)
                ->setId($row->id)
                ->setAgent_Id($row->agent_id)
                ->setArea_Id($row->area_id)
                ->setFrom_date($row->from_date)
                ->setTo_Date($row->to_date)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setOrder($row->order)
                ->setEvent($row->event)
                ->setFactor($row->factor)
                ->setProduct_Id($row->product_id)
                ->setProvince_Id($row->province_id)
                ->setDistrict_Id($row->district_id)
                ->setIsDeleted($row->isdeleted);
            $entries[] = $entry;
        }
      
        return $entries;
        
    }
}
